package com.sinosoft.lk.core.server;

import com.sinosoft.lk.core.config.SsoServerConfig;
import com.sinosoft.lk.core.constants.SsoConstants;
import com.sinosoft.lk.core.helper.JedisHelper;
import com.sinosoft.lk.core.utils.StringUtils;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Protocol.Command;

/**
 * @author 绫小路
 * @date 2021/3/13 21:07
 * @description
 */
public class SsoServerHelper {

  private static Logger logger = LoggerFactory.getLogger(SsoServerHelper.class);
  private SsoServerConfig ssoServerConfig;

  private boolean responseIsJson;
  private String serverName;
  private String ssoSessionIdName;

  public boolean isResponseIsJson() {
    return responseIsJson;
  }

  public void setResponseIsJson(boolean responseIsJson) {
    this.responseIsJson = responseIsJson;
  }

  public String getServerName() {
    return serverName;
  }

  public void setServerName(String serverName) {
    this.serverName = serverName;
  }

  public String getSsoSessionIdName() {
    return ssoSessionIdName;
  }

  public void setSsoSessionIdName(String ssoSessionIdName) {
    this.ssoSessionIdName = ssoSessionIdName + ":";
  }

  public SsoServerConfig getSsoServerConfig() {
    return ssoServerConfig;
  }

  public void setSsoServerConfig(SsoServerConfig ssoServerConfig) {
    this.ssoServerConfig = ssoServerConfig;
    //1、set Argument
    responseIsJson = ssoServerConfig.isJson();
    serverName = ssoServerConfig.getServerName();
    ssoSessionIdName = ssoServerConfig.getSsoSessionIdName();

    //2、valid
    if (StringUtils.isEmpty(ssoSessionIdName)) {
      ssoSessionIdName = SsoConstants.SESSION_ID;
    }

    //3、init redis
    JedisHelper.setConfig(
        ssoServerConfig.getJedisAddress(),
        ssoServerConfig.getJedisPoolConfig(),
        ssoServerConfig.getJedisConnectionTimeout()
    );
    JedisHelper instance = JedisHelper.getInstance();
    if (instance.ping()) {
      logger.info("--------------init jedisUtils instance finish success !------------------");
      //logger.error("connect redis fail! please check config!");
      //throw new Exception("connect redis fail! please check config!");
    }

  }

  public SsoServerHelper() {
    serverName = SsoConstants.SERVER_NAME;
    ssoSessionIdName = SsoConstants.SESSION_ID;
  }

 /* public void login(String token, Map<String, Object> userInfo, int seconds) {
    JedisHelper.setObjectHash(token, userInfo, seconds);
  }*/

  public void loginStr(String token, Map<String, String> userInfo, int seconds) {
    JedisHelper.setStrHash(token, userInfo, seconds);
  }

  public void logout(String token){
    JedisHelper.del(token);
  }

  public Map<String,String> getLoginUserInfo(String token){
    return JedisHelper.getStrHash(token);
  }

  public int deleteToken(String token) {
    return JedisHelper.del(token).intValue();
  }

  public boolean hasToken(String token) {
    return JedisHelper.exists(token);
  }

  /**
   * 获取token剩余时间
   *
   * @param token
   * @return seconds 秒
   */
  public Long ttlToken(String token) {
    return JedisHelper.ttl(token);
  }

  public void clearAllToken() {
    Object o = JedisHelper.sendCommand(Command.KEYS, "*");
    if (o != null) {
      List<byte[]> list = (List<byte[]>) o;
      String[] keys = new String[list.size()];
      for (int i = 0; i < list.size(); i++) {
        keys[i] = new String(list.get(i));
      }
      JedisHelper.dels(keys);
    }
  }

}
